package ar.com.kique.persistence.dao.impl;

import java.util.List;

import org.hibernate.*;
import org.hibernate.cfg.Configuration;
import org.hibernate.service.ServiceRegistry;
import org.hibernate.service.ServiceRegistryBuilder;

import ar.com.kique.model.CentroCosto;
import ar.com.kique.model.Empleado;
import ar.com.kique.model.SolicitudGasto;
import ar.com.kique.persistence.dao.SolicitudGastoDAO;




public class SolicitudGastoDAOHibernate implements SolicitudGastoDAO {

	private static SessionFactory  sessionFactory = configureSessionFactory();
	private static ServiceRegistry serviceRegistry;

	private static SessionFactory configureSessionFactory() throws HibernateException {
	    Configuration configuration = new Configuration();
	    configuration.configure();
	    serviceRegistry = new ServiceRegistryBuilder().applySettings(configuration.getProperties()).buildServiceRegistry();        
	    sessionFactory = configuration.buildSessionFactory(serviceRegistry);
	    return sessionFactory;
	}
	  
	@Override
	public SolicitudGasto persistir(SolicitudGasto solicitud) {
		Session session = sessionFactory.openSession();
	    session.beginTransaction();
	    session.saveOrUpdate(solicitud);
	    session.getTransaction().commit();
	    session.close();
	    return solicitud;
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<SolicitudGasto> buscarSolicitudes(Empleado empleado,
			CentroCosto cCosto) {
		Session session = sessionFactory.openSession();
		Query query = session.createQuery("select sg from " + " SolicitudGasto sg"
		        + " where sg.centroCosto = :cCosto AND sg.empleado = :empleado");
		query.setParameter("empleado", empleado);
		query.setParameter("cCosto", cCosto);
		return (List<SolicitudGasto>) query.list();
		
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<SolicitudGasto> buscarSolicitudes(Empleado empleado) {
		Session session = sessionFactory.openSession();
		Query query = session.createQuery("select sg from " + " SolicitudGasto sg"
		        + " where sg.empleado = :empleado");
		query.setParameter("empleado", empleado);
		return (List<SolicitudGasto>) query.list();
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<SolicitudGasto> buscarSolicitudes(CentroCosto cCosto) {
		Session session = sessionFactory.openSession();
		Query query = session.createQuery("select sg from " + " SolicitudGasto sg"
		        + " where sg.centroCosto = :cCosto");
		query.setParameter("cCosto", cCosto);
		return (List<SolicitudGasto>) query.list();
	}

	@Override
	public SolicitudGasto buscarSolicitud(Integer id) {
		 Session session = sessionFactory.openSession();
		    return (SolicitudGasto) session.get(SolicitudGasto.class, id);
		
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<SolicitudGasto> listarPendientes() {
		Session session = sessionFactory.openSession();
	    Query query = session.createQuery("select sg from " + " SolicitudGasto sg"
	        + " where sg.estado = :estado");
	    query.setParameter("estado", SolicitudGasto.PENDIENTE);
	    return (List<SolicitudGasto>) query.list();

	}

}
